package xiaofei.library.hermestest;

import xiaofei.library.hermes.Hermes;
import xiaofei.library.hermes.stub.StubCallback;
import xiaofei.library.hermestest.service.BooleanServiceAbility;
import xiaofei.library.hermestest.service.ByteServiceAbility;
import xiaofei.library.hermestest.service.DoubleServiceAbility;
import xiaofei.library.hermestest.service.FloatServiceAbility;
import xiaofei.library.hermestest.service.IntServiceAbility;
import xiaofei.library.hermestest.service.StringServiceAbility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

public class HermesOhosTest {
    private static final int RANDOM_C = 10;
    private static final byte NUMBER = 2;

    @Test
    public void testHermesBoolean() {
        Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        AbilitySlice slice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

        Hermes.handleBoolean(BooleanServiceAbility.class.getName(), slice, false, false,
                (StubCallback<String>) value1 -> {
                    assertTrue(Boolean.parseBoolean(value1));
                });
    }

    @Test
    public void testHermesInt() {
        Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        AbilitySlice slice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

        Hermes.handleInt(IntServiceAbility.class.getName(), slice,
                RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                    assertTrue(RANDOM_C == Integer.valueOf(value));
                });
    }

    @Test
    public void testHermesString() {
        Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        AbilitySlice slice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

        Hermes.handleString(StringServiceAbility.class.getName(), slice,
                "01", "20", (StubCallback<String>) value -> {
                    assertTrue("0102".equals(String.valueOf(value)));
                });
    }

    @Test
    public void testHermesDouble() {
        Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        AbilitySlice slice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

        Hermes.handleDouble(DoubleServiceAbility.class.getName(), slice,
                RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                    assertTrue(RANDOM_C == Double.valueOf(value));
                });
    }

    @Test
    public void testHermesFloat() {
        Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        AbilitySlice slice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

        Hermes.handleFloat(FloatServiceAbility.class.getName(), slice,
                RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                    assertTrue((RANDOM_C + RANDOM_C) == Float.valueOf(value));
                });
    }

    @Test
    public void testHermesByte() {
        Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        AbilitySlice slice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

        byte[] targets = new byte[NUMBER];
        Hermes.handleByte(ByteServiceAbility.class.getName(), slice,
                targets[0], targets[0], (StubCallback<String>) value1 -> {
                    assertEquals("byte", value1);
                });
    }

}